//
//  ZCRaisePicCell.m
//  zctx
//
//  Created by apple on 15/11/30.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#define ItemWidth 55
#define ItemHeight 55
#define MaxItemCount 3

#import "ZCRaisePicCell.h"
@interface ZCRaisePicCell ()
@property(nonatomic,strong) NSMutableArray *itemArray;
@property (nonatomic,strong)NSMutableArray *photoMenuItems;
@end
@implementation ZCRaisePicCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.maxNum = MaxItemCount;
        [self addLabelTitle];
        [self setup];
    }
    return self;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style cellID:(NSString *)cellId{
    self = [super initWithStyle:style reuseIdentifier:cellId];
    if (self) {
        self.maxNum = 1;
        [self addErrorLabelTitle];
        [self setupError];
    }
    return self;
}

-(void)setPicArray:(NSMutableArray *)PicArray{
    _PicArray = PicArray;
    [self initlizerScrollView:self.PicArray];
}
- (void)setup{

    _photoScrollView = [[UIView alloc]init];
    [self.contentView addSubview:_photoScrollView];
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        [_photoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.bottom).with.offset(-13);
            make.height.mas_equalTo(@(63));
            make.left.equalTo(self.contentView.left).with.offset(20);
            make.right.equalTo(self.contentView.right).with.offset(-40);
        }];
    }else{
        [_photoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.bottom).with.offset(-13);
            make.height.mas_equalTo(@(63));
            make.left.equalTo(self.contentView.left).with.offset(20);
            make.right.equalTo(self.contentView.right).with.offset(-40);

        }];
    }
    _photoMenuItems = [[NSMutableArray alloc]init];
    _itemArray = [[NSMutableArray alloc]init];
    
}

- (void)setupError{
    
    _photoScrollView = [[UIView alloc]init];
    [self.contentView addSubview:_photoScrollView];
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        [_photoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.labelTitle.top).with.offset(0);
            make.height.mas_equalTo(@(63));
            make.left.equalTo(self.labelTitle.right).with.offset(5);
            make.right.equalTo(self.contentView.right).with.offset(-40);
        }];
    }else{
        [_photoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.labelTitle.top).with.offset(0);
            make.height.mas_equalTo(@(63));
            make.left.equalTo(self.labelTitle.right).with.offset(5);
            make.right.equalTo(self.contentView.right).with.offset(-40);
            
        }];
    }
    _photoMenuItems = [[NSMutableArray alloc]init];
    _itemArray = [[NSMutableArray alloc]init];
    
}


-(void)messagePhotoItemView:(MessagePhotoMenuItem *)messagePhotoItemView didSelectDeleteButtonAtIndex:(NSInteger)index{
    [self.PicArray removeObjectAtIndex:index];
    self.picBlock(self.PicArray);
    [self initlizerScrollView:self.PicArray];
}

-(void)initlizerScrollView:(NSArray *)imgList{
    [self.photoScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat  itmWidth;
    
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        itmWidth = 45;
    }else{
        itmWidth = 55;
    }
    
    for(int i=0;i<imgList.count;i++){
        
       UIImage *image = [imgList objectAtIndex:i];
        
        MessagePhotoMenuItem *photoItem = [[MessagePhotoMenuItem alloc]initWithFrame:CGRectMake(i * (itmWidth+11),0, itmWidth, 63)];
        photoItem.delegate = self;
        photoItem.tag = i;
        photoItem.contentImage = image;
        [self.photoScrollView addSubview:photoItem];
        [self.itemArray addObject:photoItem];
    }
    if(imgList.count<self.maxNum){
        UIButton *btnphoto=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnphoto setFrame:CGRectMake( (itmWidth+11) * imgList.count,10, itmWidth, itmWidth)];//
        if (imgList.count==0) {
            [btnphoto setImage:[UIImage imageNamed:@"raise_pic"] forState:UIControlStateNormal];
        }else{
            [btnphoto setImage:[UIImage imageNamed:@"raise_add"] forState:UIControlStateNormal];
        }
        btnphoto.tag = imgList.count;
        //给添加按钮加点击事件
        [btnphoto addTarget:self action:@selector(openMenu:) forControlEvents:UIControlEventTouchUpInside];
        [self.photoScrollView addSubview:btnphoto];
    }
}

-(void)openMenu:(UIButton*)sender{
    //在这里呼出下方菜单按钮项

    if ([self.delegate respondsToSelector:@selector(didSelectePhotoMenuItem:atIndex:)]) {
        NSInteger index = sender.tag;
        NSLog(@"self.PicArray.count = %ld,index = %ld",(unsigned long)self.PicArray.count,(long)index);
        [self.delegate didSelectePhotoMenuItem:self.PicArray atIndex:index];
    }
}

- (void)addErrorLabelTitle {
    [self.contentView addSubview:self.labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.top).with.offset(15);
        make.height.mas_equalTo(@(21));
        make.width.mas_equalTo(@(65));
        make.left.mas_equalTo(@(18));
    }];
    self.labelTitle.text = @"产品图片:";
    self.labelTitle.textColor = [UIColor colorWithHexString:@"#404040"];
}

- (void)addLabelTitle {
    [self.contentView addSubview:self.labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.top).with.offset(11);
        make.height.mas_equalTo(@(21));
        make.width.mas_equalTo(@(75));
        make.left.mas_equalTo(@(20));
    }];
}

- (void)addOptionalLable {
    [self.contentView addSubview:self.optionalLable];
    [self.optionalLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.right.mas_equalTo(@(-20));
        make.width.mas_equalTo(@(30));
    }];
}


- (UILabel *)labelTitle {
    if (!_labelTitle) {
        _labelTitle = [[UILabel alloc] init];
        [_labelTitle setTextColor:[UIColor colorWithHexString:@"#404040"]];
        _labelTitle.font = [UIFont systemFontOfSize:14];
        _labelTitle.text = @"产品图片：";
    }
    return _labelTitle;
}
- (UILabel *)optionalLable {
    if (!_optionalLable) {
        _optionalLable = [[UILabel alloc] init];
        _optionalLable.text = @"选填";
        _optionalLable.textAlignment = NSTextAlignmentRight;
        _optionalLable.font = [UIFont systemFontOfSize:15];
        [_optionalLable setTextColor:[UIColor colorWithHexString:@"#a3a3a3"]];
    }
    return _optionalLable;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
